<?php include show_tpl('public_uheader.htm'); ?>

<div style="width:100%; height:20px; background:#E9ECF3;"></div>

<?php include show_tpl('public_usider.htm'); ?>
<div id="page-wrapper" class="gray-bg">

    <div class="row wrapper wrapper-content">

    </div>
</div>
</div>
<?php include show_tpl('public_ufooter.htm'); ?>
<script type="text/html" id="cateDemo">
    <a class="layui-btn layui-btn-xs" lay-event="detail">管理价格段</a>
    <a class="layui-btn layui-btn-xs" lay-event="del">删除店铺</a>
</script>
<script type="text/html" id="goodsDemo">
    <a class="layui-btn layui-btn-xs" lay-event="detail">管理商品</a>
    <a class="layui-btn layui-btn-xs" lay-event="del">删除价格区间</a>
</script>
<script type="text/html" id="goodsPDemo">
    <a class="layui-btn layui-btn-xs" lay-event="del">删除商品</a>
</script>
<script>
    var cateTable;
    var goodsTable;
    var goodsPTable;
    var form;
    var indexPrice;
    layui.use('form', function() {
        form = layui.form;
        form.render();
        form.on('submit(addCateFilter)', function(data){
            $.ajax({
                type: "post",
                data:data.field,
                async: false,
                url: "<?php echo url('user-cateAdd'); ?>",
                success: function (data) {
                    if (data.status){
                        layer.msg(data.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                        cateTable.reload();
                        layer.closeAll();
                    }else {
                        layer.msg(data.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                    }
                }
            });
            return false;
        });
        form.on('submit(addPriceFilter)', function(data){
            $.ajax({
                type: "post",
                data:data.field,
                async: false,
                url: "<?php echo url('user-priceAdd'); ?>",
                success: function (data) {
                    if (data.status){
                        layer.msg(data.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                        goodsTable.reload();
                        layer.close(indexPrice);
                    }else {
                        layer.msg(data.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                    }
                }
            });
            return false;
        });
        form.on('submit(addGoodsFilter)', function(data){
            $.ajax({
                type: "post",
                data:data.field,
                async: false,
                url: "<?php echo url('user-goodsAdd'); ?>",
                success: function (data) {
                    if (data.status){
                        layer.msg(data.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                        goodsPTable.reload();
                        layer.close(indexPrice);
                    }else {
                        layer.msg(data.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                    }
                }
            });
            return false;
        });
    });

    $("#cateAdd").on('click',function () {
        var index = layer.open({
            type: 1,
            title: '添加店铺',
            id: 'addCate',
            area: ['360px'],
            anim: 1
            ,content: '<form class="layui-form layui-form-pane">\n' +
                '    <div class="layui-form-item">\n' +
                '        <label class="layui-form-label">店铺名字</label>\n' +
                '        <div class="layui-input-block">\n' +
                '            <input type="text" name="name" lay-verify="required" placeholder="只能为字母和数字，下划线_及破折号-" autocomplete="off" class="layui-input">\n' +
                '        </div>\n' +
                '    </div>\n' +
                '    <div class="layui-form-item">\n' +
                '        <div class="layui-input-block">\n' +
                '            <button class="layui-btn" lay-submit lay-filter="addCateFilter">立即提交</button>\n' +
                '        </div>\n' +
                '    </div>\n' +
                '    <!-- 更多表单结构排版请移步文档左侧【页面元素-表单】一项阅览 -->\n' +
                '</form>\n' +
                '\n' +
                '\n'
        });

        form.render();
    });



    layui.use('table', function(){
        var table = layui.table;
        cateTable = table.render({
            elem: '#cate'
            ,height: 312
            ,url: '/user-storeCate.htm' //数据接口
            ,cols: [[ //表头
                {type:'numbers',title: '序列号', width:60}
                ,{field: 'name', title: '商铺名', width:200,edit:'text'}
                ,{fixed: 'right',width:200, align:'left', title: '操作', toolbar: '#cateDemo'}
            ]]
        });
        table.on('tool(cate_filter)', function(obj){
            var data = obj.data;
            var layEvent = obj.event;
            if(layEvent === 'detail'){
                layer.open({
                    id:'goodsPDiv',
                    type: 1,
                    title: '价格区间',
                    area: ['600px', '300px'],
                    anim: 1
                    ,content:'<button class="layui-btn layui-btn-fluid" id="priceAdd" data-id="'+obj.data.id+'">添加价格段</button><div id="goods" lay-filter="goods_filter"></div>'
                });
                goodsTable = table.render({
                    elem: '#goods'
                    ,url: "<?php echo url('user-goods'); ?>"
                    ,where: data
                    ,cols: [[
                        {type:'numbers',title: '序列号', width:60}
                        ,{field: 'price_start', title: '起始价格', width: 200,edit:'text'}
                        ,{field: 'price_end', title: '结束价格', width: 200,edit:'text'}
                        ,{fixed: 'right', align:'left',width: 200, toolbar: '#goodsDemo'}
                    ]]
                });
                $("#priceAdd").on('click',function () {
                    var cid = $(this).attr('data-id');
                    indexPrice = layer.open({
                        type: 1,
                        title: '添加价格区间',
                        id: 'addPrice',
                        area: ['360px'],
                        anim: 1
                        ,content: '<form class="layui-form layui-form-pane">\n' +
                            '    <div class="layui-inline">\n' +
                            '    <label class="layui-form-label">价格范围</label>\n' +
                            '    <div class="layui-input-inline" style="width: 100px;">\n' +
                            '      <input type="text" name="price_start" placeholder="￥最初价格" autocomplete="off" class="layui-input">' +
                            '<input type="hidden" name="cid" value="'+cid+'" class="layui-input">\n' +
                            '    </div>\n' +
                            '    <div class="layui-form-mid">-</div>\n' +
                            '    <div class="layui-input-inline" style="width: 100px;">\n' +
                            '      <input type="text" name="price_end" placeholder="￥结束价格" autocomplete="off" class="layui-input">\n' +
                            '    </div>\n' +
                            '  </div>\n' +
                            '    <div class="layui-form-item">\n' +
                            '        <div class="layui-input-block">\n' +
                            '            <button class="layui-btn" lay-submit lay-filter="addPriceFilter">立即提交</button>\n' +
                            '        </div>\n' +
                            '    </div>\n' +
                            '    <!-- 更多表单结构排版请移步文档左侧【页面元素-表单】一项阅览 -->\n' +
                            '</form>\n' +
                            '\n' +
                            '\n'
                    });
                    form.render();
                });
            } else if(layEvent === 'del'){
                layer.confirm('真的删除店铺吗？', function(index){
                    layer.close(index);
                    $.ajax({
                        type: "post",
                        url: "<?php echo url('user-storeDel'); ?>",
                        data: data,
                        async: false,
                        success: function(result) {
                            if (result.status === true) {
                                obj.del();
                                layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                            } else {
                                layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i>');
                            }
                        }
                    });

                });
            }
        });

        table.on('edit(cate_filter)', function(obj){
            var data ={};
            data.id = obj.data.id;
            data.name = obj.field;
            data.value = obj.value;
            $.ajax({
                type: "post",
                url: "<?php echo url('user-storeUpdate'); ?>",
                data: data,
                async: false,
                success: function(result) {
                    if (result.status === true) {
                        layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                    } else {
                        cateTable.reload();
                        layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i>');
                    }
                }
            });
        });
        table.on('tool(goods_filter)', function(obj){
            var data = obj.data;
            var layEvent = obj.event;
            if(layEvent === 'detail'){
                layer.open({
                    id:'goodsDiv',
                    type: 1,
                    title: '商品管理',
                    area: ['600px', '300px'],
                    anim: 1
                    ,content:'<button class="layui-btn layui-btn-fluid" id="goodsAdd" data-id="'+obj.data.id+'" data-idx="'+obj.data.cid+'">添加商品</button><div id="goodsP" lay-filter="goodsP_filter"></div>'
                });
                goodsPTable = table.render({
                    elem: '#goodsP'
                    ,url: "<?php echo url('user-goodsP'); ?>"
                    ,where: data
                    ,cols: [[
                        {type:'numbers',title: '序列号', width:60}
                        ,{field: 'name', title: '商品名', width: 200,edit:'text'}
                        ,{fixed: 'right', align:'left',width: 100, toolbar: '#goodsPDemo'}
                    ]]
                });
                $("#goodsAdd").on('click',function () {
                    var pid = $(this).attr('data-id');
                    var cid = $(this).attr('data-idx');
                    indexPrice = layer.open({
                        type: 1,
                        title: '添加商品',
                        id: 'addGoods',
                        area: ['360px'],
                        anim: 1
                        ,content: '<form class="layui-form layui-form-pane">\n' +
                            '    <div class="layui-form-item">\n' +
                            '        <label class="layui-form-label">商品名</label>\n' +
                            '        <div class="layui-input-block">\n' +
                            '            <input type="text" name="name" lay-verify="required" placeholder="只能为字母和数字，下划线_及破折号-" autocomplete="off" class="layui-input">\n' +
                            '            <input type="hidden" name="pid" value="'+pid+'" lay-verify="required" class="layui-input">\n' +
                            '            <input type="hidden" name="cid" value="'+cid+'" lay-verify="required"  class="layui-input">\n' +
                            '        </div>\n' +
                            '    </div>\n' +
                            '    <div class="layui-form-item">\n' +
                            '        <div class="layui-input-block">\n' +
                            '            <button class="layui-btn" lay-submit lay-filter="addGoodsFilter">立即提交</button>\n' +
                            '        </div>\n' +
                            '    </div>\n' +
                            '    <!-- 更多表单结构排版请移步文档左侧【页面元素-表单】一项阅览 -->\n' +
                            '</form>\n' +
                            '\n' +
                            '\n'
                    });
                    form.render();
                })
            } else if(layEvent === 'del'){
                layer.confirm('真的删除价格区间吗？', function(index){
                    layer.close(index);
                    $.ajax({
                        type: "post",
                        url: "<?php echo url('user-goodsDel'); ?>",
                        data: data,
                        async: false,
                        success: function(result) {
                            if (result.status === true) {
                                obj.del();
                                layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                            } else {
                                layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i>');
                            }
                        }
                    });

                });
            }
        });
        table.on('edit(goods_filter)', function(obj){
            var data ={};
            data.id = obj.data.id;
            data.cid = obj.data.cid;
            data.name = obj.field;
            data.value = obj.value;
            $.ajax({
                type: "post",
                url: "<?php echo url('user-goodsUpdate'); ?>",
                data: data,
                async: false,
                success: function(result) {
                    if (result.status === true) {
                        layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                    } else {
                        cateTable.reload();
                        layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i>');
                    }
                }
            });
        });

        table.on('tool(goodsP_filter)', function(obj){
            var data = obj.data;
            var layEvent = obj.event;
            if(layEvent === 'del'){
                layer.confirm('真的删除吗？', function(index){
                    layer.close(index);
                    $.ajax({
                        type: "post",
                        url: "<?php echo url('user-goodsPDel'); ?>",
                        data: data,
                        async: false,
                        success: function(result) {
                            if (result.status === true) {
                                obj.del();
                                layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                            } else {
                                layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i>');
                            }
                        }
                    });
                });
            }
        });

        table.on('edit(goodsP_filter)', function(obj){
            var data ={};
            data.id = obj.data.id;
            data.cid = obj.data.cid;
            data.pid = obj.data.pid;
            data.name = obj.field;
            data.value = obj.value;
            $.ajax({
                type: "post",
                url: "<?php echo url('user-goodsPUpdate'); ?>",
                data: data,
                async: false,
                success: function(result) {
                    if (result.status === true) {
                        layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i> ');
                    } else {
                        cateTable.reload();
                        layer.msg(result.data+'<i class="layui-icon layui-icon-face-smile" style="font-size: 30px; color: #1E9FFF;"></i>');
                    }
                }
            });
        });
    });


</script>
